package servlet; 

import java.io.File;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;

import util.hibernate.HibernateUtil;

public class DBStartUpServlet extends HttpServlet {
	
	private static final long serialVersionUID = 1L;
	
	
	/** Initializing the Logger */
	
	protected static final Logger logger=Logger.getLogger(DBStartUpServlet.class);
	
	public void init(ServletConfig config) throws ServletException {
		
		JDBCAuto.process();
		
		System.out.println("\n**** Initializing Hibernate Init Servlet ********** \n");
		super.init(config);
		
		
		//This is for local properties.
		String cfgDir = "C:\\xampp\\tomcat\\webapps\\bodado\\WEB-INF\\classes";
		
		logger.info("config dir:"+cfgDir);
		initLogPro(cfgDir);
		
		try {
			HibernateUtil.appHome = cfgDir;
			HibernateUtil.configure();
		} catch (Exception e) {
			e.printStackTrace();
		}
		
	}
	
	/** Handles the requests from http client.
	* @param request servlet request
	* @param response servlet response
	*/
	protected void service(HttpServletRequest request,HttpServletResponse response) throws ServletException, java.io.IOException {
	}
	
	
	private void initLogPro(String path){
		try {
			PropertyConfigurator.configure(path+File.separatorChar+"log4j.properties");
		} catch (Exception ex) {
			System.out.println("Log4J can not be initialized");
			logger.info("Log4J can not be initialized");
		}
	}	


}